<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/1
 * Time: 10:15
 */
namespace Admin\Controller;

use Admin\Model\BaseinfoVersionModel;
use Base\Controller\CurdController;
use Library\Utils;

class DoseinfoController extends CurdController
{
    public function __construct()
    {
        parent::__construct(true, true);

    }
    protected function editAfter($data)
    {
        $datanames   = M('dimdailyiteminfo')->where(array('id'=>$data['dosenameID'],'status'=>1))->getField('name');
        $datacontents = M('dimdailyiteminfo')->where(array('id'=>$data['contentID'],'status'=>1))->getField('name');
        if($datacontents){
            $data['name']=$data['value'].$datanames.'/'.$datacontents;
        }else{
            $data['name']=$data['value'].$datanames;
        }
        $BaseinfoVersionModel=new BaseinfoVersionModel();
        $controller = hump2underline(CONTROLLER_NAME);
        $infos=$BaseinfoVersionModel->where(array('name'=>$controller))->find();
        $data['version']=$infos['version'];
        return $data;
    }
    /**
     * 获取所有数据并注入模板
     * */
    protected function getAll()
    {
        if($_REQUEST['keyword']){
            $map['name'] = array('like','%'.$_REQUEST['keyword'].'%');
            $map['_logic'] = 'or';
            $where['_complex'] = $map;
        }
        if(!I('get.del')){
            $where['status']=1;
        }else{
            $where['status']=0;
        }
        if($_REQUEST['selects']){
            $limit=$_REQUEST['selects'];
        }else{
            $limit=20;
        }
        $data = $this->model->pageToView($limit, true,$where,'','id asc');
        $this->assign("page", $data['page']);
        $this->assign("list", $data['list']);
    }

    /**
     *添加药品
     *
     */
    public  function  add(){
     $data =$this->model->create();
     if(!$data){
         $this->error($this->model->getError());
      }else{
         $dataname = M('dimdailyiteminfo')->where(array('id'=>$data['dosenameID'],'status'=>1))->getField('name');
         if(!empty($data['contentID'])){
             $datacontent = M('dimdailyiteminfo')->where(array('id'=>$data['contentID'],'status'=>1))->getField('name');
         }
         if($datacontent){
             $data['name']=$data['value'].$dataname.'/'.$datacontent;
         }else{
             $data['name']=$data['value'].$dataname;
         }
//         dump();
         $data = $this->addAfter($data);
         $add = $this->model->add($data);
         $this->addEnd($add);
     }
    }
    protected function addAfter($data)
    {
        $BaseinfoVersionModel=new BaseinfoVersionModel();
        $controller = hump2underline(CONTROLLER_NAME);
        $infos=$BaseinfoVersionModel->where(array('name'=>$controller))->find();
        $data['version']=$infos['version'];
        $ids=$this->model->order('id desc')->find();
        $data['id']=$ids['id']+1;
        return $data;
    }

}